<?php


namespace app\shop\service\user;


use app\shop\model\user\AdminRoleModel;
use app\shop\model\user\DauModel;
use app\shop\service\BaseService;

class DauService extends BaseService
{
    protected static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new DauModel();
        }
        return self::$instance;
    }

    public function __construct()
    {
    }

    /**
     *设置日活用户
     * @param int $userId
     * @return bool
     * @throws \Exception
     */
    public static function setDauData(int $userId): bool
    {
        try {
            $day = strtotime(date('Y-m-d'));
            $info = self::getInstance()
                ->where('user_id', $userId)
                ->where('day', $day)
                ->find();
            if ($info) {
                return true;
            }
            $info = self::getInstance();
            $info->user_id = $userId;
            $info->day = $day;
            return $info->save();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

}